package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.model.Model4;
import com.sam.im.heiqun.uis.adapters.CountryExpandAdapter;
import com.google.gson.Gson;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;

public class CountrySelectActivity extends BaseSwipeBackActivity implements ExpandableListView.OnGroupClickListener ,CountryExpandAdapter.IChildItemClick {

    private static final String TAG = CountrySelectActivity.class.getSimpleName();
    @BindView(R.id.expandable_listview)
    ExpandableListView mListView;

    private CountryExpandAdapter adapter;
    private Model4 model4;

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.select_county_and_area);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        initData();
        adapter = new CountryExpandAdapter(model4, this);
        mListView.setAdapter(adapter);

        adapter.setiChildItemClick(this);
        mListView.setOnGroupClickListener(this);

        mListView.setGroupIndicator(null);

        int groupCount = adapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            mListView.expandGroup(i);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_country_select;
    }


    private void initData() {
        try {
            String countryJson = getResources().getString(R.string.country_list);
            countryJson = countryJson.replace(" ","");
            Gson gson = new Gson();
            model4 = gson.fromJson(countryJson, Model4.class);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        return true;
    }

    @Override
    public void itemClickDo(String country,String mobileCode) {
        Intent data = new Intent();
        data.putExtra("country",country);
        data.putExtra("mobileCode","+"+mobileCode);
        setResult(RESULT_OK, data);
        finish();
    }
}
